package com.whmskj.xjlhbc.core;

import cn.hutool.core.util.NumberUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Page {

    public static void startPage(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
        Integer pageNum = NumberUtil.parseInt(request.getParameter("pageNum"),1);
            Integer pageSize = NumberUtil.parseInt(request.getParameter("pageSize"),10);
        PageHelper.startPage(pageNum, pageSize);
    }

    public static AjaxResult getDataTable(List<?> list) {
        Map<String, Object> map = new HashMap<>();
        map.put("results", list);
        map.put("total", PageInfo.of(list).getTotal());
        return AjaxResult.success(map);
    }
}
